package com.book.servlet.manage;

import com.book.service.BookService;
import com.book.service.impl.BookServiceImpl;
import com.book.utils.ThymeleafUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.thymeleaf.context.Context;

import java.io.IOException;

@WebServlet(name = "AddBorrowServlet", value = "/addBorrow")
public class AddBorrowServlet extends HttpServlet {
    BookService bookService;

    @Override
    public void init() throws ServletException {
        bookService = new BookServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Context context = new Context();
        context.setVariable("bookList", bookService.getActiveBookList());
        context.setVariable("studentList", bookService.getStudentList());
        ThymeleafUtil.process("add-borrow.html", context, response.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int sid = Integer.parseInt(request.getParameter("student"));
        int bid = Integer.parseInt(request.getParameter("book"));
        bookService.addBorrow(sid,bid);
        response.sendRedirect("index");
    }
}
